from django.contrib import admin
from goods.models import Goods, SKU, SKUImage
# Register your models here.
from . import models
from celery_tasks.html.tasks import generate_static_list_search_html, generate_static_sku_detail_html


# 添加后台站点管理
class GoodsAdmin(admin.ModelAdmin):
    list_display = ['name', 'id', 'sales']

    # 添加保存操作
    def save_model(self, request, obj, form, change):
        obj.save()
        # 调用异步任务
        generate_static_list_search_html.delay()

    # 删除操作
    def delete_model(self, request, obj):
        # obj.delete()
        print(123)


class SKUAdmin(admin.ModelAdmin):

    # 添加保存操作
    def save_model(self, request, obj, form, change):
        obj.save()
        # 调用异步任务
        generate_static_list_search_html.delay()

    # 删除操作
    def delete_model(self, request, obj):
        # obj.delete()
        print(123)


# 图片模型类管理对象
class SKUImageAdmin(admin.ModelAdmin):

    # 添加保存操作
    def save_model(self, request, obj, form, change):
        # print(type(obj.image))
        # print(obj.image.url) 图片完整路径
        obj.save()
        # 调用异步任务
        generate_static_sku_detail_html.delay(obj.sku.id)

    # 删除操作
    def delete_model(self, request, obj):
        generate_static_sku_detail_html.delay(obj.sku.id)
        # obj.delete()


admin.site.register(Goods, GoodsAdmin)
admin.site.register(SKU, SKUAdmin)
admin.site.register(SKUImage, SKUImageAdmin)
